package com.hzzh.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hzzh.dao.EmployeeDAO;
import com.hzzh.vo.Employee;

/**
 * Servlet implementation class SelectEmployeesOfDeptServlet
 */
@WebServlet("/SelectEmployeesOfDeptServlet")
public class SelectEmployeesOfDeptServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SelectEmployeesOfDeptServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int departmentid = Integer.parseInt(request.getParameter("departmentid"));
		EmployeeDAO dao = new EmployeeDAO();
		
			List<Employee> employeesList = dao.selectEmployeesByDept(departmentid);
			
			//将查询得到的员工信息，以XML文档的格式返回到浏览器
			response.setContentType("text/xml;charset=utf-8");
			PrintWriter out = response.getWriter();
			response.setHeader("Cache-Control", "no-cache");
			out.println("<?xml version='1.0' encoding='" + "utf-8" + "'?>");
			
			//符合xml规范，有根节点，否则解析有问题
			out.println("<employee>");
			for(Employee e:employeesList){
				out.println("<option>");
				out.println("<value>" + e.getEmployeeid()+"</value>");
				out.println("<text>" + e.getEmployeename() + "</text>");
				out.println("</option>");
			}
			out.println("</employee>");
			out.close();
		
		
	}

}
